﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Common.Interfaces;
using Kiiro.Validation.Interfaces;
namespace Kiiro.Validation.Validators.Entities
{
    public class DiscussionValidator : BaseEntityValidator, IValidator<Discussion>
    {
        private readonly List<ValidationMessage> validationErrors = new List<ValidationMessage>();

        #region  IValidator<Discussion> Members
        public List<ValidationMessage> Validate(Discussion entityToValidate)
        {
            // If the entity is marked as deleted, don't do any validation, just return.
            if ( entityToValidate.EditType != EditType.Deleted )
            {
                ValidationMessage message = null;
                
                #region Required Fields Checks

                message = IsRequired(entityToValidate.Title, "Title");

                if (message != null)
                {
                    entityToValidate.TitleMessage.Add(message);
                    validationErrors.Add(message);
                }
                
                #endregion

                #region Comparison Checks

                if ( !string.IsNullOrEmpty ( entityToValidate.Text ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.Title, "Discussion Title" );

                    if ( message != null )
                    {
                        entityToValidate.TitleMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.Title, "Discussion Title" );

                    if ( message != null )
                    {
                        entityToValidate.TitleMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = CheckMaximumLength ( 5000, entityToValidate.Text, "Discussion Details" );

                    if ( message != null )
                    {
                        entityToValidate.TextMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    //message = HasScriptingTagsInText ( entityToValidate.Text, "Discussion Details" );

                    //if ( message != null )
                    //{
                    //    entityToValidate.TextMessage.Add ( message );
                    //    validationErrors.Add ( message );
                    //}
                }
                #endregion

                #region Attachments Validation
                    if (entityToValidate.AttachmentList != null && entityToValidate.AttachmentList.Count > 0)
                    {
                        if (entityToValidate.AttachmentList.Find(x => x.EditType == EditType.Unchanged) != null)
                        {
                            foreach (Attachment file in entityToValidate.AttachmentList)
                            {
                                message = AreAttachmentsValidExtensions(entityToValidate, file);

                                if (message != null)
                                {
                                    entityToValidate.AttachmentListMessage.Add(message);
                                    validationErrors.Add(message);
                                }
                            }
                        }
                    }
                #endregion
            }

            return validationErrors;
        }
        #endregion
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Validation/Validators/Entities/DiscussionValidator.cs $
 * 
 * 8     12/14/09 2:10p Shafia
 * attachments file extensions validation for discussions, tasks, and
 * issues
 * 
 * 7     10/26/09 10:42a Adamw
 * 
 * 6     10/23/09 1:41p Adamw
 * Case 332, script tag check
 * 
 * 5     17/08/09 10:45a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/